import Axios from "axios";
import router from "../../router/index.js";

//响应拦截
Axios.interceptors.response.use(
  response => {
    return response;
  },
  async error => {
    return Promise.reject(error);
  }
);

//允许跨域携带cookie信息
Axios.defaults.withCredentials = true;
const fetch = {
  get: function(url) {
    return new Promise((resolve, reject) => {
      Axios.get(url)
        .then(
          ({ data }) => {
            resolve(data);
          },
          err => {
            reject(err);
          }
        )
        .catch(error => {
          reject(error);
        });
    });
  },
  post: function(url, params, config) {
    return new Promise((resolve, reject) => {
      Axios.post(url, params, config)
        .then(
          ({ data }) => {
            resolve(data);
          },
          err => {
            reject(err);
          }
        )
        .catch(error => {
          reject(error);
        });
    });
  },
  put: function(url, params) {
    return new Promise((resolve, reject) => {
      Axios.put(url, params)
        .then(
          ({ data }) => {
            resolve(data);
          },
          err => {
            reject(err);
          }
        )
        .catch(error => {
          reject(error);
        });
    });
  },
  delete: function(url, params) {
    return new Promise((resolve, reject) => {
      Axios.delete(url, params)
        .then(
          ({ data }) => {
            resolve(data);
          },
          err => {
            reject(err);
          }
        )
        .catch(error => {
          reject(error);
        });
    });
  }
};

export default fetch;